<?php

namespace Admin\Controller;

class CommonController extends \Think\Controller {

    public function login() {
        $this->display();
    }

    public function login_action() {
        sleep(2);  //模拟延时  
        //以下的参数不用到就不需要去定义
        $data['flag'] = true;                                             //值只有 true 或者 false
        $data['msg'] = "成功信息, 迟些再改为自定义样式弹出层";                 //成功的时候才填该参数
        $data['redirect'] = U('admin/index/index');                        //有值的时候页面才会重定向
        $data['error'] = "错误信息, 迟些再改为自定义样式弹出层";               //错误的时候才填该参数   
        $data['callback'] = null;                                         //需要页面有回调函数时才填该参数 可以为 js语句 或者 页面中的方法,如:  CheckLogin() 
        exit(json_encode($data));
    }

    public function lock() {
        $this->display();
    }

    public function login_up() {

//        $array_cookie['id'] = 1;
//        $array_cookie['adm_name'] = 'admin';
//        $array_cookie['power'] = 1;
//        cookie('admin', $array_cookie);             //保存cookie
//
//        $data['flag'] = true;
//        //$data['msg']="登陆成功";                  
//        $data['redirect'] = U('admin/index/index');
//        die(json_encode($data));
        


/////////////////////


        $nick = I('nick');
        $password = md5(I('password'));

        $user = M('admin')->where(array('adm_name' => $nick))->find();  //查询用户信息

        if (!$user || $user['adm_pwd'] != $password) {    //判断用户名，密码是否正确
            $data['flag'] = false; //值只有 true 或者 false
            $data['error'] = "用户名或密码不匹配";                //错误的时候才填该参数                     
            exit(json_encode($data));
        } else {
            $array_cookie['id'] = $user['id'];
            $array_cookie['adm_name'] = $user['nick'];
            $array_cookie['power'] = $user['power'];
            cookie('admin', $array_cookie);             //保存cookie

            $data['flag'] = true;
            //$data['msg']="登陆成功";                  
            $data['redirect'] = U('admin/index/index');
            exit(json_encode($data));
        }
    }

    //退出登录
    public function logout() {
        cookie('admin', null);
        $data['flag'] = true;
        $data['redirect'] = U('admin/common/login');
        exit(json_encode($data));
    }

}
